package com.capgemini.gae.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.capgemini.gae.daos.PersistableObject;
import com.google.appengine.api.datastore.Key;

@Entity
public class Tournament implements PersistableObject {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Key id;

	private String name;
	private String location;
	private int edition;

	@Temporal(TemporalType.DATE)
	private Date startDate;

	@Temporal(TemporalType.DATE)
	private Date endDate;

	@OneToMany(mappedBy = "tournament", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Collection<Performance> performances;

	@Enumerated(EnumType.STRING)
	private Discipline sport;

	public Tournament() {

	}

	public Tournament(String name, String location, int edition,
			Date startDate, Date endDate, Discipline sport) {
		super();
		this.name = name;
		this.location = location;
		this.edition = edition;
		this.startDate = startDate;
		this.endDate = endDate;
		this.sport = sport;
		this.performances = new ArrayList<Performance>();
	}

	public Long getId() {
		if (id != null) {
			return id.getId();
		} else
			return null;
	}

	public void setId(Key id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public int getEdition() {
		return edition;
	}

	public void setEdition(int edition) {
		this.edition = edition;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Collection<Performance> getPerformances() {
		return performances;
	}

	public void addPerformance(Performance performance) {
		this.performances.size();
		this.performances.add(performance);
	}

	public Discipline getSport() {
		return sport;
	}

	public void setSport(Discipline sport) {
		this.sport = sport;
	}

	@Override
	public Class getEntityClass() {
		return Tournament.class;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + edition;
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((location == null) ? 0 : location.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((performances == null) ? 0 : performances.hashCode());
		result = prime * result + ((sport == null) ? 0 : sport.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Tournament other = (Tournament) obj;
		if (edition != other.edition)
			return false;
		if (endDate == null) {
			if (other.endDate != null)
				return false;
		} else if (!endDate.equals(other.endDate))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (location == null) {
			if (other.location != null)
				return false;
		} else if (!location.equals(other.location))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (performances == null) {
			if (other.performances != null)
				return false;
		} else if (!performances.equals(other.performances))
			return false;
		if (sport != other.sport)
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Tournament [id=" + id + ", name=" + name + ", location="
				+ location + ", edition=" + edition + ", startDate="
				+ startDate + ", endDate=" + endDate + ", performances="
				+ performances + ", sport=" + sport + "]";
	}

	@Override
	public Key getKey() {
		return id;
	}

}
